package com.atguigu.gmall.user.service.impl;

import com.atguigu.gmall.common.util.MD5;
import com.atguigu.gmall.model.user.LoginVo;
import com.atguigu.gmall.model.user.UserInfo;
import com.atguigu.gmall.user.mapper.UserInfoMapper;
import com.atguigu.gmall.user.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/5/20 14:48
 * @Version 1.0
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public UserInfo login(LoginVo loginVo) {
        //  select * from user_info where (login_name = ? or email = ? or phone_num = ?) and passwd = ?;
        LambdaQueryWrapper<UserInfo> userInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //  构建sql 语句.
        //  默认eq 是 and 关系.
        userInfoLambdaQueryWrapper.or(queryWrapper->{
            queryWrapper.eq(UserInfo::getLoginName,loginVo.getLoginName()).or()
                    .eq(UserInfo::getEmail,loginVo.getLoginName()).or()
                    .eq(UserInfo::getPhoneNum,loginVo.getLoginName());
        });
        //  做加密处理.
        String newPwd = MD5.encrypt(loginVo.getPasswd());
        userInfoLambdaQueryWrapper.eq(UserInfo::getPasswd,newPwd);
        //  返回查询对象
        return userInfoMapper.selectOne(userInfoLambdaQueryWrapper);
    }
}
